import 'package:ladder_cash/app/app.dart';
import 'package:ladder_cash/app/page/login/launch_page.dart';
import 'package:ladder_cash/app/router_manage.dart';
import 'package:ladder_cash/generated/l10n.dart';
import 'package:ladder_cash/value/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';

import 'Global.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await App.init();
  await SystemChrome.setPreferredOrientations(<DeviceOrientation>[
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]).then((_) => runApp(MyApp()));
}
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
  // 存储当前语言的状态
  Locale _locale = Locale('en', 'US');
  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.light,
        systemNavigationBarIconBrightness: Brightness.light));
    App.appContext = context;
    LWidgetInit(context);
    return ScreenUtilInit(
        designSize: const Size(375, 812),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (context, child) => GetMaterialApp(
            navigatorObservers: [App.routeObserver],
            navigatorKey: Global.navigatorKey,
            theme: ThemeData(
              primaryColor: AppColors.primaryColor,
              fontFamily: 'Regular', // 设置默认字体
            ),
            title: "ladder_cash",
            defaultTransition: Transition.fadeIn,
            localizationsDelegates: const [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              S.delegate
            ],
            supportedLocales: S.delegate.supportedLocales,
            locale: _locale,  // 设置初始语言为英语（美国）
            home: LaunchPage(switchLanguage:_switchLanguage),
            builder: EasyLoading.init(
                builder: (context, widget) => MediaQuery(
                      data:
                          MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                      child: widget!,
                    )),
            getPages: RouterManage.pages));
  }

  void _switchLanguage(Locale newLocale) {
    setState(() {
      _locale = newLocale;
    });
  }
}
